package com.groovy.domain

import org.junit.Test

class ForTest {


    @Test
    void testRang() {
        //循环输出0到10的数字,1..10是一个Range范围，同Kotlin的语法一致
        def range = 1..10
        println(range[0])//获取第一个数组
        println(range.contains(10))//判断是否存在10
        println(range.from)//获取第一个值
        println(range.to)//获取最后一个值
    }

    @Test
    void testFor1() {
        //循环输出0到10的数字,1..10是一个Rang范围，同Kotlin的语法一致
        for (i in 0..10) {
            println(i)
        }
    }

    @Test
    void testFor2() {
        //对一个map进行循环,默认LinkedHashMap,as HashMap表示LinkedHashMap转换成HashMap
        for (map in [userName: '周伯通', age: 90] as HashMap) {
            println("key:${map.key},value:${map.value}")
        }
    }

    //初始化数据
    static def initUsers() {
        def users = [
                "No1": [userName: "周伯通", sex: '男', age: 100],
                "No2": [userName: "瑛姑", sex: '女', age: 67],
                "No3": [userName: "小龙女", sex: '女', age: 30],
                "No4": [userName: "杨过", sex: '男', age: 20],
                "No5": [userName: "黄老邪", sex: '男', age: 60]
        ]
        return users
    }

    @Test
    void testEach1() {
        def users = initUsers()
        //循环
        users.each { user ->
            println("key=${user.key},value=${user.value}")
        }
    }

    @Test
    void testEach2() {
        def users = initUsers()
        //循环遍历，带索引的循环
        users.eachWithIndex { def user, int index ->
            println("index=${index},key=${user.key},value=${user.value}")
        }
    }


}
